﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNet.Utilities;

namespace taobaoSpider
{
    /// <summary>
    /// 对模板类进行建模
    /// http://zxn.taobao.com/template_list.htm?=&productType=0&site=0&industry=0&p=2
    /// </summary>
    public class HtmlTemplate
    {
        ShowErrorLogEventHandler ShowMessageHandler = null;

        String _template_idnum { get; set; }
        String _nickname { get; set; }
        string _sellnum { get; set; }
        String _userVer { get; set; }
        string _price { get; set; }
        String _title { get; set; }
        string _hanye { get; set; }
        System.Data.DataSet _ds = null;

        string _templateIDXpath = "";

        public System.Data.DataSet DS
        {
            get { return _ds; }
        }

        public HtmlTemplate(ShowErrorLogEventHandler showMessageHandler,string templateIDXpath)
        {
            this._templateIDXpath = templateIDXpath;
            this.ShowMessageHandler = showMessageHandler;

            this._ds = new System.Data.DataSet();
            System.Data.DataTable dt = new System.Data.DataTable("template");
            this._ds.Tables.Add(dt);
            this._ds.Tables["template"].Columns.Add("template_idnum", typeof(string));
            this._ds.Tables["template"].Columns.Add("nickname", typeof(string));
            this._ds.Tables["template"].Columns.Add("sellnum", typeof(int));
            this._ds.Tables["template"].Columns.Add("userVer", typeof(string));
            this._ds.Tables["template"].Columns.Add("price", typeof(double));
            this._ds.Tables["template"].Columns.Add("title", typeof(string));
            this._ds.Tables["template"].Columns.Add("hanye", typeof(string));
        }

        public void InitTemplatePage(int pageIndex)
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem();
            ShowErrorLogEventArgs e = new ShowErrorLogEventArgs();
            item.URL = "http://zxn.taobao.com/template_list.htm?=&productType=0&site=0&industry=0&p="+pageIndex.ToString();
            HtmlAgilityPack.HtmlDocument doc = GlobalFun.ParaseToHtml(http.GetHtml(item).Html);
            HtmlAgilityPack.HtmlNodeCollection designerHnc = doc.DocumentNode.SelectNodes(this._templateIDXpath);
            if (designerHnc != null)
            {
                foreach (HtmlAgilityPack.HtmlNode node in designerHnc)
                {
                    try
                    {
                        string templateItemUrl = node.SelectNodes("descendant::dl/descendant::dt/descendant::a")[0].Attributes["href"].Value.ToString();
                        this._template_idnum = templateItemUrl.Split('=')[1];
                        item.URL = templateItemUrl;
                        //item.URL = "http://zxn.taobao.com/detail.htm?spm=0.0.0.0.IJhlU3&id=20897442";
                        HtmlAgilityPack.HtmlDocument templateDoc = GlobalFun.ParaseToHtml(http.GetHtml(item).Html);
                        HtmlAgilityPack.HtmlNodeCollection templateDetailHnc = templateDoc.DocumentNode.SelectNodes(ReadIniFile.IniReadValue("template", "templateDetail"));
                        if (templateDetailHnc != null)
                        {
                            HtmlAgilityPack.HtmlNodeCollection saleInfoHnc = templateDetailHnc[0].SelectNodes("descendant::div[@class='sale-info']");
                            this._title = saleInfoHnc[0].SelectNodes("descendant::h2")[0].InnerText.Trim();
                            this._sellnum = saleInfoHnc[0].SelectNodes("descendant::dd")[0].InnerText.Trim();
                            this._userVer = saleInfoHnc[0].SelectNodes("descendant::dd")[1].InnerText.Trim();
                            if (saleInfoHnc[0].InnerHtml.Contains("class=\"contents original\""))
                            {
                                this._price = saleInfoHnc[0].SelectNodes("descendant::span[@class='price-s-show']")[0].InnerText.Trim();
                            }
                            else
                            {
                                this._price = saleInfoHnc[0].SelectNodes("descendant::span[@class='price-s-show']")[0].InnerText.Trim();
                            }
                            this._nickname = templateDetailHnc[0].SelectNodes("descendant::div[@class='designer']")[0].InnerText.Trim().Split(':')[1].Trim();
                            System.Data.DataRow dr = this._ds.Tables["template"].NewRow();
                            dr["template_idnum"] = this._template_idnum;
                            dr["nickname"] = this._nickname;
                            dr["sellnum"] = this._sellnum;
                            dr["userVer"] = this._userVer;
                            dr["price"] = this._price;
                            dr["title"] = this._title;
                            dr["hanye"] = this._hanye;
                            _ds.Tables["template"].Rows.Add(dr);

                            e.ErrPosition = "HtmlTemplate:InitTemplatePage(int pageIndex)";
                            e.ErrMessage = "第 " + pageIndex.ToString() + " 页--模板ID<<" + this._template_idnum + ">>";
                            System.Windows.Forms.Label lblInfo = new System.Windows.Forms.Label();
                            lblInfo.Name = "lblInfo";
                            this.ShowMessageHandler(lblInfo, e);
                        }
                    }
                    catch (Exception ex)
                    {
                        GlobalFun.WriteErrorLog("HtmlTemplate:InitTemplatePage(int pageIndex)=>catch", this._template_idnum + "--" + ex.Message);
                    }
                }
            }
        }
    }
}
